package com.aidong.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CalendarUtil {
	
	/**
	 * 计算时间差
	 * 结果返回毫秒数
	 * @param nowDate
	 * @param courseDate
	 * @return
	 * @throws Exception 
	 */
	 public static long fromDateStringToLong(String nowDate, String courseDate) throws Exception {
			  DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			  
			  Date now = df.parse(nowDate);
				 
			  Date course = df.parse(courseDate);
			  
			  long diff = course.getTime() - now.getTime(); // 这样得到的差值是微秒级别
			  
			  long minute = diff / (1000 * 60);
			 
			  return minute;
	 } 

	/**
	 * 获取指定日期是星期几
	 * @param date
	 *            当前时间
	 * @return
	 */
	public static Map<String, Object> getArrangeMent(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, 1);// 把日期往后增加一天.整数往后推,负数往前移动
		Date dateDay = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		String strDay = sdf.format(dateDay);
		
		String week = getWeekOfDate(date);
		String weekend = null;
		String weekendNum = null;
		if(week.equals("星期日")){
			weekend = "一";
			weekendNum = "1";
		}else if(week.equals("星期一")){
			weekend = "二";
			weekendNum = "2";
		}else if(week.equals("星期二")){
			weekend = "三";
			weekendNum = "3";
		}else if(week.equals("星期三")){
			weekend = "四";
			weekendNum = "4";
		}else if(week.equals("星期四")){
			weekend = "五";
			weekendNum = "5";
		}else if(week.equals("星期五")){
			weekend = "六";
			weekendNum = "6";
		}else if(week.equals("星期六")){
			weekend = "日";
			weekendNum = "7";
		}
		Map<String, Object> dateMap = new HashMap<String, Object>();
		dateMap.put("dateDay", strDay);
		dateMap.put("week", weekend);
		dateMap.put("weekNum", weekendNum);
		return dateMap;
	}

	/**
	 * 获取指定日期是星期几
	 * @param date
	 * @return
	 */
	public static String getWeekOfDate(Date date) {
		String[] weekOfDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar calendar = Calendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		}
		int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0) {
			w = 0;
		}
		return weekOfDays[w];
	}
	
	/**
	 * 时间安排表
	 * @return
	 */
	public static List<Map<String, Object>> getCalenderWeek(){
		List<Map<String, Object>> resultMap = new ArrayList<Map<String,Object>>();
		try {
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(new Date());
			calendar.add(calendar.DATE, -1);
			Date dateDay = calendar.getTime();
			Map<String, Object> one = CalendarUtil.getArrangeMent(dateDay);//当天的日期，和是周几
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
			Map<String, Object> two = CalendarUtil.getArrangeMent(sdf.parse(one.get("dateDay").toString()));
			Map<String, Object> three = CalendarUtil.getArrangeMent(sdf.parse(two.get("dateDay").toString()));
			Map<String, Object> four = CalendarUtil.getArrangeMent(sdf.parse(three.get("dateDay").toString()));
			Map<String, Object> five = CalendarUtil.getArrangeMent(sdf.parse(four.get("dateDay").toString()));
			Map<String, Object> six = CalendarUtil.getArrangeMent(sdf.parse(five.get("dateDay").toString()));
			Map<String, Object> seven = CalendarUtil.getArrangeMent(sdf.parse(six.get("dateDay").toString()));
			
			resultMap.add(one);
			resultMap.add(two);
			resultMap.add(three);
			resultMap.add(four);
			resultMap.add(five);
			resultMap.add(six);
			resultMap.add(seven);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}
	/**
	 * 时间安排表
	 * 十一月活动需要
	 * @return
	 */
	public static List<Map<String, Object>> getCalenderWeekNew(){
		List<Map<String, Object>> resultMap = new ArrayList<Map<String,Object>>();
		try {
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(new Date());
			SimpleDateFormat sm=new SimpleDateFormat("yyyy-MM-dd");
			Date date=sm.parse("2016-11-11");
			
			if(calendar.getTime().before(date)){
				calendar.setTime(date);
			}
			
			calendar.add(calendar.DATE, -1);
			Date dateDay = calendar.getTime();
			Map<String, Object> one = CalendarUtil.getArrangeMent(dateDay);//当天的日期，和是周几
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
			Map<String, Object> two = CalendarUtil.getArrangeMent(sdf.parse(one.get("dateDay").toString()));
			Map<String, Object> three = CalendarUtil.getArrangeMent(sdf.parse(two.get("dateDay").toString()));
			Map<String, Object> four = CalendarUtil.getArrangeMent(sdf.parse(three.get("dateDay").toString()));
			Map<String, Object> five = CalendarUtil.getArrangeMent(sdf.parse(four.get("dateDay").toString()));
			Map<String, Object> six = CalendarUtil.getArrangeMent(sdf.parse(five.get("dateDay").toString()));
			Map<String, Object> seven = CalendarUtil.getArrangeMent(sdf.parse(six.get("dateDay").toString()));
			
			resultMap.add(one);
			resultMap.add(two);
			resultMap.add(three);
			resultMap.add(four);
			resultMap.add(five);
			resultMap.add(six);
			resultMap.add(seven);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}
	/**
	 * 周期数据转换
	 * @return
	 */
	public static int dataConversion(String week){
		int weekend = 0;
		if(week.equals("日")){
			weekend = 7;
		}else if(week.equals("一")){
			weekend = 1;
		}else if(week.equals("二")){
			weekend = 2;
		}else if(week.equals("三")){
			weekend = 3;
		}else if(week.equals("四")){
			weekend = 4;
		}else if(week.equals("五")){
			weekend = 5;
		}else if(week.equals("六")){
			weekend = 6;
		}
		return weekend;
	}
	
	/**
	 * 预约报名时间格式转换
	 * @return
	 * @throws Exception 
	 */
	public static String dateFomat(String appointDateTime) throws Exception{
		SimpleDateFormat sdfDate =  new SimpleDateFormat("yyyy年MM月dd日");
		Date date = sdfDate.parse(appointDateTime);
		
		SimpleDateFormat sdfStr = new SimpleDateFormat("yyyy-MM-dd");
		String dateTime = sdfStr.format(date);
		return dateTime;
	}
	
	/**
	 * 团操预约时间格式转换
	 * @return
	 * @throws Exception 
	 */
	public static String courseDateFomat(String appointDateTime) throws Exception{
		if(appointDateTime.contains("年")){
			SimpleDateFormat sdfDate =  new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
			Date date = sdfDate.parse(appointDateTime);
			SimpleDateFormat sdfStr = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String dateTime = sdfStr.format(date);
			return dateTime;
		}else{
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date beginDate = dft.parse(appointDateTime);
			return dft.format(beginDate);
		}
	}
	
	/**
	 * 判断待付款状态的订单是否已超时
	 * @param payTime
	 * @return
	 * @throws Exception 
	 */
	public static int payOverTimeCancle(int createdTime,String orderType) throws Exception{
		int expire_time=0;//秒数
		if("1".equals(orderType) || "2".equals(orderType)){
			expire_time=60*10;//课程和活动    10分钟  
		}else{
			expire_time=60*60*2;//商品2小时
		}
		//expire_time = 60 * 10;
		if(createdTime >= expire_time){
			return 0;
		}else{
			return expire_time - createdTime;
		}
		
	}
	/**
	 * 判断待付款状态的订单是否已超时
	 * @param payTime
	 * @return
	 * @throws Exception 
	 */
	public static Map<String, Object> payOverTimeCancleNew(String payTime,String orderType) throws Exception{
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date payFormatTime = sim.parse(payTime);
		long payMseCondes = payFormatTime.getTime();
		
		Date sysDate = new Date(); // 系统当前时间
		long sysMseCondes = sysDate.getTime();
		int minute=0;//秒数
		long ssdiffers = (sysMseCondes - payMseCondes) / (1000); // 相差秒数
		if("1".equals(orderType) || "2".equals(orderType)){
			minute=600+60;//课程和活动    10分钟    延长1分钟
		}else{
			minute=60*60*2+60;//商品2小时  延长1分钟
		}
		if(ssdiffers > minute){ 
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("ssdiffers", ssdiffers);
			resultMap.put("flag", true);
			return resultMap;
		}
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("ssdiffers", minute - ssdiffers);
		resultMap.put("flag", false);
		return resultMap;
	}
	
	public static String dealDate(String date, String time) throws Exception {
		String result;
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
		Date passedDate = dateformat.parse(date);
		Date curDate = dateformat.parse(dateformat.format(new Date()));
		if (passedDate.equals(curDate)) {
			SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
			result = sf.format(sf.parse(time));
			return result;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(passedDate);
		cal.add(Calendar.DATE, 1);

		if (cal.getTime().equals(curDate)) {
			result = "昨天";

		} else {
			result = date;
		}

		return result;
	}
	
	/**
	 * 餐饮购买时间安排
	 * @return
	 */
	public static List<Map<String, Object>> getBuyDate() {
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		
		Date date = new Date();
		String applyDate = new SimpleDateFormat("yyyyMM").format(date);
		String pageDate = new SimpleDateFormat("MM-dd").format(date);
		String yearDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
		Map<String, Object> dateMap = new HashMap<String, Object>();
		dateMap.put("week", "今天");
		dateMap.put("applyDate", applyDate);
		dateMap.put("pageDate", pageDate);
		dateMap.put("yearDate", yearDate);
		maps.add(dateMap);
		
		Date date1 = getNextDay(date,1);
		String week1 = getWeekOfDate(date1);
		String applyDate1 = new SimpleDateFormat("yyyyMM").format(date1);
		String pageDate1 = new SimpleDateFormat("MM-dd").format(date1);
		String yearDate1 = new SimpleDateFormat("yyyy-MM-dd").format(date1);
		Map<String, Object> dateMap1 = new HashMap<String, Object>();
		dateMap1.put("week", week1);
		dateMap1.put("applyDate", applyDate1);
		dateMap1.put("pageDate", pageDate1);
		dateMap1.put("yearDate", yearDate1);
		maps.add(dateMap1);
		Date date2 = getNextDay(date,2);
		String week2 = getWeekOfDate(date2);
		String applyDate2 = new SimpleDateFormat("yyyyMM").format(date2);
		String pageDate2 = new SimpleDateFormat("MM-dd").format(date2);
		String yearDate2 = new SimpleDateFormat("yyyy-MM-dd").format(date2);
		Map<String, Object> dateMap2 = new HashMap<String, Object>();
		dateMap2.put("week", week2);
		dateMap2.put("applyDate", applyDate2);
		dateMap2.put("pageDate", pageDate2);
		dateMap2.put("yearDate", yearDate2);
		maps.add(dateMap2);
		
		Date date3 = getNextDay(date,3);
		String week3 = getWeekOfDate(date3);
		String applyDate3 = new SimpleDateFormat("yyyyMM").format(date3);
		String pageDate3 = new SimpleDateFormat("MM-dd").format(date3);
		String yearDate3 = new SimpleDateFormat("yyyy-MM-dd").format(date3);
		Map<String, Object> dateMap3 = new HashMap<String, Object>();
		dateMap3.put("week", week3);
		dateMap3.put("applyDate", applyDate3);
		dateMap3.put("pageDate", pageDate3);
		dateMap3.put("yearDate", yearDate3);
		maps.add(dateMap3);
		
		Date date4 = getNextDay(date,4);
		String week4 = getWeekOfDate(date4);
		String applyDate4 = new SimpleDateFormat("yyyyMM").format(date4);
		String pageDate4 = new SimpleDateFormat("MM-dd").format(date4);
		String yearDate4 = new SimpleDateFormat("yyyy-MM-dd").format(date4);
		Map<String, Object> dateMap4 = new HashMap<String, Object>();
		dateMap4.put("week", week4);
		dateMap4.put("applyDate", applyDate4);
		dateMap4.put("pageDate", pageDate4);
		dateMap4.put("yearDate", yearDate4);
		maps.add(dateMap4);
		
		/*int monthEqual1 = compare_monuth(yearDate, yearDate1);
		int monthEqual2 = compare_monuth(yearDate, yearDate2);
		if (monthEqual1 == 0 &&  monthEqual1 == 0) {
			maps.add(dateMap1);
			maps.add(dateMap2);
		}if (monthEqual1 == 0 && monthEqual2 == 1) {
			maps.add(dateMap1);
		}*/
		return maps;
	}
	
	
	/**
	 * 餐饮购买时间安排
	 * @return
	 */
	public static List<Map<String, Object>> getBuyDateFood() {
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		
		Date date = new Date();
		/*String applyDate = new SimpleDateFormat("yyyyMM").format(date);
		String pageDate = new SimpleDateFormat("MM-dd").format(date);
		String yearDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
		Map<String, Object> dateMap = new HashMap<String, Object>();
		dateMap.put("week", "今天");
		dateMap.put("applyDate", applyDate);
		dateMap.put("pageDate", pageDate);
		dateMap.put("yearDate", yearDate);
		maps.add(dateMap);*/
		
		Date date1 = getNextDay(date,1);
		String week1 = getWeekOfDate(date1);
		String applyDate1 = new SimpleDateFormat("yyyyMM").format(date1);
		String pageDate1 = new SimpleDateFormat("MM-dd").format(date1);
		String yearDate1 = new SimpleDateFormat("yyyy-MM-dd").format(date1);
		Map<String, Object> dateMap1 = new HashMap<String, Object>();
		dateMap1.put("week", week1);
		dateMap1.put("applyDate", applyDate1);
		dateMap1.put("pageDate", pageDate1);
		dateMap1.put("yearDate", yearDate1);
		maps.add(dateMap1);
		Date date2 = getNextDay(date,2);
		String week2 = getWeekOfDate(date2);
		String applyDate2 = new SimpleDateFormat("yyyyMM").format(date2);
		String pageDate2 = new SimpleDateFormat("MM-dd").format(date2);
		String yearDate2 = new SimpleDateFormat("yyyy-MM-dd").format(date2);
		Map<String, Object> dateMap2 = new HashMap<String, Object>();
		dateMap2.put("week", week2);
		dateMap2.put("applyDate", applyDate2);
		dateMap2.put("pageDate", pageDate2);
		dateMap2.put("yearDate", yearDate2);
		maps.add(dateMap2);
		
		Date date3 = getNextDay(date,3);
		String week3 = getWeekOfDate(date3);
		String applyDate3 = new SimpleDateFormat("yyyyMM").format(date3);
		String pageDate3 = new SimpleDateFormat("MM-dd").format(date3);
		String yearDate3 = new SimpleDateFormat("yyyy-MM-dd").format(date3);
		Map<String, Object> dateMap3 = new HashMap<String, Object>();
		dateMap3.put("week", week3);
		dateMap3.put("applyDate", applyDate3);
		dateMap3.put("pageDate", pageDate3);
		dateMap3.put("yearDate", yearDate3);
		maps.add(dateMap3);
		
		Date date4 = getNextDay(date,4);
		String week4 = getWeekOfDate(date4);
		String applyDate4 = new SimpleDateFormat("yyyyMM").format(date4);
		String pageDate4 = new SimpleDateFormat("MM-dd").format(date4);
		String yearDate4 = new SimpleDateFormat("yyyy-MM-dd").format(date4);
		Map<String, Object> dateMap4 = new HashMap<String, Object>();
		dateMap4.put("week", week4);
		dateMap4.put("applyDate", applyDate4);
		dateMap4.put("pageDate", pageDate4);
		dateMap4.put("yearDate", yearDate4);
		maps.add(dateMap4);
		
		Date date5 = getNextDay(date,5);
		String week5 = getWeekOfDate(date5);
		String applyDate5 = new SimpleDateFormat("yyyyMM").format(date5);
		String pageDate5 = new SimpleDateFormat("MM-dd").format(date5);
		String yearDate5 = new SimpleDateFormat("yyyy-MM-dd").format(date5);
		Map<String, Object> dateMap5 = new HashMap<String, Object>();
		dateMap5.put("week", week5);
		dateMap5.put("applyDate", applyDate5);
		dateMap5.put("pageDate", pageDate5);
		dateMap5.put("yearDate", yearDate5);
		maps.add(dateMap5);
		
		/*int monthEqual1 = compare_monuth(yearDate, yearDate1);
		int monthEqual2 = compare_monuth(yearDate, yearDate2);
		if (monthEqual1 == 0 &&  monthEqual1 == 0) {
			maps.add(dateMap1);
			maps.add(dateMap2);
		}if (monthEqual1 == 0 && monthEqual2 == 1) {
			maps.add(dateMap1);
		}*/
		return maps;
	}
	
	public static Date getNextDay(Date date, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, +day); //+1今天的时间加一天
		date = calendar.getTime();
		return date;
	}
	
	public static int compare_monuth(String date1, String date2) {
		int month1 = Integer.parseInt(date1.substring(5, 7));
		int month2 = Integer.parseInt(date2.substring(5, 7));
		if (month1 == month2) {
			return 0;
		} else {
			return 1;
		}
	}
	
	public static void main(String[] args) {
		//getCalenderWeek();
		
		/*try {
			getCalenderWeekNew();
			//payOverTimeCancleNew("2016-08-31 18:00:00","3");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		try{
			
			System.out.println(fromDateStringToLong("2016-11-16 10:24","2016-11-16 10:30"));
		}catch(Exception e){
			
		}
	}
	
	
}
